<script type="text/javascript">
$(document).ready(function() {
    var initial_width;
    //初始化完成
    $('#snapshot_table').jqGrid({
        url: '/grid_snapshot',
        styleUI: 'Bootstrap',
        datatype: 'json',
        mtype: 'POST',
        colNames: ['编号', '[["label path"|translate]]', '[["label description"|translate]]','卷名称', "[['label owner'|translate]]",
            '[["label Creation time"|translate]]', '[["label operation"|translate]]'
        ],
        colModel: [{
            name: 'id',
            index: 'id',
            width: 3,
            align: 'left',
            hidden: true
        }, {
            name: 'name',
            index: 'name',
            width: 8,
            align: 'left',
            formatter: function(cellvalue, option, row) {
                var outerWidth = $('#snapshot_table_name').width() - 20;
                return '<a style="display:inline-block;text-overflow:ellipsis; white-space:nowrap;overflow:hidden;width:' + outerWidth + 'px;" href="#" onclick="detail_open(\'' + option.gid + '\',\'' + option.rowId + '\')">' + cellvalue + '</a>';
            },
            unformat: function(cellvalue) {
                return cellvalue;
            }
        }, {
            name: 'description',
            index: 'description',
            width: 8,
            align: 'left',
            formatter: function(cellvalue, option, row) {
                var outerWidth = $('#snapshot_table_name').width() - 40;
                return '<span style="display:inline-block;text-overflow:ellipsis; white-space:nowrap;overflow:hidden;width:' + outerWidth + 'px;">' + cellvalue + '</span>';
            },
            unformat: jqUnformat,
        }, {
            name: 'volumeName',
            index: 'volumeName',
            width: 8,
            align: 'left',
            hidden: true
        }, {
            name: 'username',
            index: 'username',
            width: 6,
            align: 'left'
        }, {
            name: 'created_at',
            index: 'created_at',
            width: 12,
            align: 'left'
        }, {
            name: 'service',
            index: 'service',
            width: 20,
            align: 'left',
            sortable: false,
            detailHidden: true,
            formatter: function(cell, option, row) {
                var html = '';
                html += '<button style="margin-left:10px;" title="克隆存储卷" class="btn btn-blue" onclick=confirm_snapshot_clone(' + row[0] + ',"' + row[1] + '")>克隆存储卷</button>';
                html += '<button style="margin-left:10px;" title="回滚" class="btn btn-blue" onclick=confirm_snapshot_rollback(' + row[0] + ',"' + row[1] + '")>回滚</button>';
                html += '<button style="margin-left:10px;" title="删除" class="btn btn-blue" onclick=confirm_snapshot_delete(' + row[0] + ',"' + row[1] + '")>删除</button>';
                return html;
            }
        }],
        page: 1,
        rowNum: 10,
        rowList: [10, 20, 30],
        sortname: 'id',
        sortorder: 'desc',
        viewrecords: true,
        //caption: '磁盘列表',
        altclass: 'ui-priority-secondary',
        altRows: true,
        autowidth: true,
        height: mainpanelHeight,
        shrinkToFit: true,
        beforeSelectRow:function(rowid, e) {
            if($(e.target).is('input')) {
                return true;
            }
            return false;
        },
        gridComplete: function() {
            var gridData = $(this).jqGrid('getRowData');
            jqgrid_page($(this), gridData);
            jqgrid_resize($(this));
        },
        resizeStart: function() {
            initial_width = $(this).width();
        },
        resizeStop: function() {
            return;
        }
    });
});

function snapshot_delete(snapshot_id) {
    $.post("/snapshot_delete", {
            'id': snapshot_id
        },
        function(data) {
            if (data.reply.is_success) {
                $('#snapshot_table').trigger("reloadGrid");
                SelfAlert("删除成功", "完成提示");
            } else {
                error = data.reply.error;
                SelfAlert("删除失败：" + error)
            }
        });
}

function confirm_snapshot_delete(snapshot_id, snapshot_name) {
    msg = '确认删除快照?';
    SelfConfirm(msg, snapshot_delete, snapshot_id, "删除提示");
}

function snapshot_rollback(snapshot_id) {
    $.post("/snapshot_rollback", {
            'id': snapshot_id
        },
        function(data) {
            if (data.reply.is_success) {
                $('#snapshot_table').trigger("reloadGrid");
                SelfAlert("回滚快照完成", "完成提示");
            } else {
                error = data.reply.error;
                SelfAlert("回滚快照失败：" + error)
            }
        });
}

function confirm_snapshot_rollback(snapshot_id, snapshot_name) {
    msg = "确认回滚快照？"
    SelfConfirm(msg, snapshot_rollback, snapshot_id, "确认提示");
}

function confirm_snapshot_clone(snapshot_id, snapshot_name) {
    angular.element('#snapshot_create').scope().open("/snapshot_clone?id=" + snapshot_id);
}
</script>
<div class="panel panel-f-style">
    <div class="panel-body">
        <div class="title-tag">
            <h3>卷快照</h3>
            <p>卷快照管理</p>
        </div>
        <div class="grid-tools">
            <div class="grid-operate pull-left">
                <button class="btn btn-blue" type="button" id="snapshot_create" ng-controller="SnapshotModalCtrl" ng-hide="true">创建</button>
            </div>
        </div>
        <div class="grid-body" id="gridttt">
            <table id="snapshot_table"></table>
        </div>
    </div>
</div>
<!--  <div class="panel panel-default">
       <div class="panel-body">
               <button id="snapshot_create" ng-controller="SnapshotModalCtrl" ng-hide="true"></button>
               <div class="item_toolsbar">
                       <span class="message_img">&nbsp;</span> 
               </div>
               <div id="div_snapshot_table" class='tab_warp'>
                   <table id="snapshot_table"></table>
               </div>
       </div> panel-body
</div>  -->
